<?php
    class AdminDthink_IndexController extends Main_Controller_Action
    {   
        public function init()
        {
            $this->loadTemplates('admin');            
        }
        
        public function preDispatch()
        {
        	$auth = Zend_Auth::getInstance();
        	if((!$auth->hasIdentity()) || ($auth->getIdentity()->level != 0))
        	{
        		if($this->_request->getActionName() != 'login')
        		{
        			$this->_redirect('/admin-dthink/index/login');
        		}
        	}
        }
        
        public function indexAction()
        {
        	$this->_redirect('admin-dthink/product');
        }
        
        public function loginAction()
        {
        	$request = new Zend_Controller_Request_Http();
        	$emailCookie = $request->getCookie('emailLogin');
        	$passwordCookie = $request->getCookie('passwordLogin');
        	$db = Zend_Registry::get('db');
        	
        	if(!empty($emailCookie))
        	{
        		// 1. Khoi tao Zend_Auth
        		$auth = Zend_Auth::getInstance();
        		
        		// 2. Khai bao bang va 2 cot se su dung so sanh trong qua trinh login
        		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
        		$authAdapter->setTableName('user')
			        		->setIdentityColumn('email')
			        		->setCredentialColumn('password');
        		
        		// 3. Dua vao so sanh voi du lieu khai bao o muc 2
        		$authAdapter->setIdentity($emailCookie);
        		$authAdapter->setCredential($passwordCookie);
        		
        		// 4. Kiem tra level cua user neu level = 0 moi duoc login
        		$select = $authAdapter->getDbSelect();
        		$select->where('level = 0 AND status = 1');
        		
        		// 5. Lay ket qua truy van
        		$result = $auth->authenticate($authAdapter);
        		$flag = false;
        		if($result)
        		{
        			// 6. Lay nhung du lieu can thiet trong bang user neu login thanh cong
        			$data = $authAdapter->getResultRowObject();
        		
        			// 7. Luu nhung du lieu cua member vao session
        			$auth->getStorage()->write($data);
        			$flag = true;
        		}
        		if($flag == true)
        		{
        			$this->_redirect('admin-dthink/product');
        		}
        		else{
        			$this->_redirect('admin-dthink/index/login');
        		}
        	}
        	else
        	{
	        	$this->_helper->layout->setLayout('login');
	        	$this->view->headTitle('Đăng nhập');
	        	
	        	if($this->_request->isPost())
	        	{
	        		// 1. Khoi tao Zend_Auth
	        		$auth = Zend_Auth::getInstance();
	        		
	        		// 2. Khai bao bang va 2 cot se su dung so sanh trong qua trinh login
	        		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
	        		$authAdapter->setTableName('user')
	        					->setIdentityColumn('email')
	        					->setCredentialColumn('password');
	        		
	        		// 3. Lay gia tri duoc gui qua tu form
	        		$email = $this->_request->getParam('email');
	        		$password = $this->_request->getParam('password');
	        		$rememberMe = $this->_request->getParam('rememberMe');
	        		
	        		if(!Zend_Validate::is($this->_request->getParam('email'), 'NotEmpty'))
	        		{
	        			$this->view->error = 'Bạn chưa nhập đủ thông tin';
	        		}
	        		else{
	        			$this->view->emailValue = $this->_request->getParam('email');
	        		}
	        		
	        		if(!Zend_Validate::is($this->_request->getParam('password'), 'NotEmpty'))
	        		{
	        			$this->view->error = 'Bạn chưa nhập đủ thông tin';
	        		}
	        		else{
	        			$this->view->passwordValue = $this->_request->getParam('password');
	        		}
	        		
	        		if(count($this->view->error) == 0)
	        		{
	        			// 4. Dua vao so sanh voi du lieu khai bao o muc 2
	        			$authAdapter->setIdentity($email);
	        			$authAdapter->setCredential(md5($password));
	        		
	        			// 5. Kiem tra level cua user neu level = 0 moi duoc login
	        			$select = $authAdapter->getDbSelect();
	        			$select->where('level = 0 AND status = 1');
	        		
	        			// 6. Lay ket qua truy van
	        			$result = $auth->authenticate($authAdapter);
	        			$flag = false;
	        			if($result->isValid())
	        			{
	        				// 7. Lay nhung du lieu can thiet trong bang user neu login thanh cong
	        				$data = $authAdapter->getResultRowObject();
	        		
	        				// 8. Luu nhung du lieu cua member vao session
	        				$auth->getStorage()->write($data);
	        				if($rememberMe == 1)
	        				{
	        					$expires = time() + 60 * 60 * 24 * 365;
	        					setcookie('emailLogin', $data->email, $expires);
	        					setcookie('passwordLogin', $data->password, $expires);
	        				}
	        				$flag = true;
	        			}
	        		
	        			if($flag == true)
	        			{
	        				$this->_redirect('admin-dthink/product');
	        			}
	        			else{
	        				$this->view->error = 'Sai tên đăng nhập hoặc mật khẩu!';
	        			}       			
	        		}
	        	}
        	}
        }
        
        public function logoutAction()
        {
            $auth = Zend_Auth::getInstance();
            $auth->clearIdentity();
            
            $request = new Zend_Controller_Request_Http();
            $emailCookie = $request->getCookie('emailLogin');
            $passwordCookie = $request->getCookie('passwordLogin');

            setcookie('emailLogin', null, -1);
            setcookie('passwordLogin', null, -1);
            
            $this->_redirect('/admin-dthink/index/login');
        }
    }
